/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import authentication.AuthenticationPort;
import authentication.EncryptionType;
import data.AccountData;
import data.email.EmailAddress;
import data.email.EmailMessage;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Třída představující SMTP účet. Podporuje odesílání zpráv.
 * @author Pavel
 */
public class SMTPAccount extends Account implements ISender {

    public SMTPAccount(AccountData data) {
        super(data);
//        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean sendEmail(EmailMessage message) throws Exception {
        String forTransport = "smtp";
        Properties props = new Properties();

        if (this.authenticationData != null) {
            props.put("mail.smtp.auth", "true");

            if (this.encryption.equals(EncryptionType.SSL)) {
                forTransport = "smtps";
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.socketFactory.fallback", "false");
            } else if (this.encryption.equals(EncryptionType.TLS)) {
                forTransport = "smtps";
                props.put("mail.smtp.starttls.enable", "true");
            }

            props.put("mail.smtp.socketFactory.port", AuthenticationPort.findPort(encryption, accountType));
        }

        props.put("mail.smtp.host", this.serverName);

        Session session = Session.getDefaultInstance(props);
        Message sendMsg = new MimeMessage(session);

        InternetAddress from = new InternetAddress(this.authenticationData.getLogin());
        sendMsg.setFrom(from);        
        for (Map.Entry<EmailAddress, Message.RecipientType> receiver : message.getEntrySet()) {
            InternetAddress internetAddrTo = new InternetAddress();
            internetAddrTo.setAddress(receiver.getKey().getAddress());
            sendMsg.addRecipient(receiver.getValue(), internetAddrTo);
        }

        sendMsg.setSubject(message.getSubject());
        sendMsg.setContent(message.getBody(), "text/html");
        sendMsg.setSentDate(message.getDate());        
        Transport t = session.getTransport(forTransport);
        //Transport.send(message);
        if (this.authenticationData != null) {
            t.connect(this.serverName, this.authenticationData.getLogin(), this.authenticationData.getPassword());
        } else {
            t.connect();
        }
        t.sendMessage(sendMsg, sendMsg.getAllRecipients());
        return true;
    }
}
